<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profile extends CI_Controller {
    
    public function index() {
        $data = array();
        $this->load->model('captcha_model');
        $img_lg = $this->captcha_model->login();
        $data['captcha_image_lg'] = $img_lg['image'];
        $this->load->view('profile/index', $data);
    }

    function reload_captcha() {
        $this->load->model('captcha_model');
        $img_lg = $this->captcha_model->login();
        echo $img_lg['image'] . '<a id="refresh-captcha" href="#">  [refresh captcha]</a>';
    }

    function lupa_password() {        
        $this->load->model('member_model');
        $phone = strip_tags($this->input->post('phone'));
        if ($phone === "") {
            $rc = "20";
            $resp = "Nomor handphone tidak boleh kosong";
        } else if ($this->session->userdata("request_lupa_pass") > 2000) {
            $rc = "20";
            $resp = "Request lupa password maksimal 2x dalam 1 hari";
        } else {
            if ($this->session->userdata("request_lupa_pass")) {                
                $this->session->set_userdata("request_lupa_pass", $this->session->userdata("request_lupa_pass") + 1);
            } else {                
                $this->session->set_userdata("request_lupa_pass",1);
            }

            if ($this->member_model->kirim_ulang_password($phone)) {
                $rc = "00";
                $resp = "Reset password berhasil dilakukan. Sandi telah berhasil dikirim ke nomor hp anda di :".$phone;
            } else {
                $this->session->set_userdata("request_lupa_pass", $this->session->userdata("request_lupa_pass") - 1);
                $rc = "20";
                $resp = "Request gagal dilakukan, No HP anda tidak terdaftar di sistem kami.";
            }
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    function daftar() {
        $this->load->model('member_model');
        $name = strip_tags($this->input->post('name'));
        $phone = strip_tags($this->input->post('phone'));
        $email = strip_tags($this->input->post('email'));
        $captcha = strip_tags($this->input->post('captcha'));

        if ($name === "") {
            $rc = "20";
            $resp = "Nama tidak boleh kosong";
        } else if ($phone === "") {
            $rc = "20";
            $resp = "No. Handphone tidak boleh kosong";
        } else if ($captcha === "") {
            $rc = "20";
            $resp = "Captcha tidak boleh kosong";
        } else if ($captcha !== $this->session->userdata("captcha_code_login")) {
            $rc = "20";
            $resp = "Captcha tidak sesuai " . $captcha . ":" . $this->session->userdata("captcha_code_login");
        } else if (($nama = $this->member_model->is_member_exist($phone)) !== "") {
            $rc = "20";
            $resp = "Member sudah terdaftar oleh akun : " . $nama . ". Silahkan klik lupa password untuk mengirimkan sandi kembali.";
        } else {
            $this->member_model->daftar_baru($name, $phone, $email);
            $rc = "00";
            $resp = "--- Pendafaran berhasil dilakukan. Silahkan tunggu, kata Sandi anda akan kami kirimkan lewat SMS ke No : " . $phone . " ---";
        }
        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

    function pre_login() {
        $data = array();
        $data["phone"] = '';
        $this->load->view("profile/pre_login", $data);
    }

    function cek_login() {        
        $this->load->model('member_model');
        $phone = strip_tags($this->input->post('phone'));
        $sandi = strip_tags($this->input->post('sandi'));

        if ($sandi === "") {
            $rc = "20";
            $resp = "Sandi tidak boleh kosong";
        } else if ($phone === "") {
            $rc = "20";
            $resp = "No. Handphone tidak boleh kosong";
        } else if ($this->member_model->cek_login($phone, $sandi) == false) {
            $rc = "20";
            $resp = "Login salah, handphone dan nomor sandi tidak cocok.";
        } else {
            $rc = "00";
            $resp = "Login berhasil";
            $this->session->set_userdata('');
        }

        echo json_encode(array("err_code" => $rc, "msg" => $resp));
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */